package com.cl.mypureframe.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.cl.mypureframe.R;
import com.cl.mypureframe.fragment.FourFragment;
import com.cl.mypureframe.fragment.HomeFragment;
import com.cl.mypureframe.fragment.ThreeFragment;
import com.cl.mypureframe.fragment.TwoFragment;
import com.cl.mypureframe.util.GLobalConstant;
import com.cl.mypureframe.util.Log;
import com.cl.mypureframe.util.UtilFile;

/**
 * @Class: MainNavigationActivity
 * @Description: 标准底部导航Activity
 * @Author: ChenLei
 * @Date: 2020/11/18---11:05
 */
public class MainNavigationActivity extends BaseActivity {
    private HomeFragment homeFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenView(R.layout.activity_navigation, R.string.app_name);

        if (savedInstanceState == null) {
            homeFragment = new HomeFragment();
            twoFragment = new TwoFragment();
            threeFragment = new ThreeFragment();
            fourFragment = new FourFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.navigation_frame, homeFragment)
                    .add(R.id.navigation_frame, twoFragment)
                    .add(R.id.navigation_frame, threeFragment)
                    .add(R.id.navigation_frame, fourFragment)
                    .commit();
        }

        onButtonClick(findViewById(R.id.nav_home));

        // app初始化方法，主要通过动态权限,加载本地各种缓存文件夹
        applyPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    protected void onPermissionSuccess() {
        // 创建本app各种缓存文件夹
        Log.i(TAG, "initAppRoot--->onSuccess");
        UtilFile.createPathFile(GLobalConstant.PHOTO_PATH);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onButtonClick(View view) {
        acInitView(findViewById(R.id.nav_home), R.mipmap.ic_launcher);
        acInitView(findViewById(R.id.nav_tjfa), R.mipmap.ic_launcher);
        acInitView(findViewById(R.id.nav_my), R.mipmap.ic_launcher);
        acInitView(findViewById(R.id.nav_more), R.mipmap.ic_launcher);

        switch (view.getId()) {
            case R.id.nav_home:
                clickTab(view, R.string.tab1, R.mipmap.ic_launcher, homeFragment);
                break;
            case R.id.nav_tjfa:
                clickTab(view, R.string.tab2, R.mipmap.ic_launcher, twoFragment);
                break;
            case R.id.nav_my:
                clickTab(view, R.string.tab3, R.mipmap.ic_launcher, threeFragment);
                break;
            case R.id.nav_more:
                clickTab(view, R.string.tab4, R.mipmap.ic_launcher, fourFragment);
                break;

        }
    }

    private void clickTab(View view, int textResid, int imgResid, Fragment fragment) {
        setTitleName(textResid);
        acView(view, imgResid);
        show(fragment);
    }

    private void show(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(homeFragment).hide(twoFragment).hide(threeFragment).hide(fourFragment)
                .show(fragment).commitAllowingStateLoss();

    }

    // 默认文字颜色 不要在这里修改颜色，去colors.xml颜色配置文件修改taboff
    private void acInitView(View view, int mipmapId) {
        activateView(view, mipmapId, getMColor(R.color.taboff));
    }

    // 选中文字颜色 不要在这里修改颜色，去colors.xml颜色配置文件修改tabon
    private void acView(View view, int mipmapId) {
        activateView(view, mipmapId, getMColor(R.color.tabon));
    }

    private void activateView(View view, int mipmapId, int colorId) {
        try {
            ViewGroup viewGroup = (ViewGroup) view;
            ((ImageView) viewGroup.getChildAt(0)).setImageResource(mipmapId);
            ((TextView) viewGroup.getChildAt(1)).setTextColor(colorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
